#!/bin/sh
func_archive_app()
{

CURTIME=`date "+%Y-%m-%d_%H-%M-%S"`
CURDIR=$(cd "$(dirname "$0")"; pwd) #shell脚本所在目录

WSDIR=$1
WSNAME=$2
SCHEME=$3
CONFIG=$4

#exportOptionsPlist 和 configuration
OPTIONS_PLIST_PATH=${CURDIR}/exportOptionsPlist/ipa_adhoc.plist
CONFIGURATION="Release"
if [ "$CONFIG" == "debug" ]
then
    CONFIGURATION="DEBUG"
    OPTIONS_PLIST_PATH=${CURDIR}/exportOptionsPlist/ipa_debug.plist
elif [ "$CONFIG" == "Enter" ]
then
    OPTIONS_PLIST_PATH=${CURDIR}/exportOptionsPlist/ipa_enter_inhouse.plist
    CONFIGURATION="Release"
fi

#archive和ipa所在目录
ARCHIVE_DIR=${WSDIR}/Packages/Archive #archieve目录
ARCHIVE_PATH=${ARCHIVE_DIR}/$SCHEME-$CONFIG.xcarchive #archieve集合文件
IPA_DIR=${WSDIR}/Packages/IPA/$SCHEME-$CONFIG-$CURTIME #ipa目录
DEF_IPA_PATH=${WSDIR}/Packages/IPA/$SCHEME-$CONFIG.ipa #拷贝出来的ipa

#创建archive
if [ ! -d "${ARCHIVE_DIR}" ]
then
echo mkdir archive dir
mkdir -p "${ARCHIVE_DIR}"
fi

#创建ipa dir
if [ ! -d "${IPA_DIR}" ]
then
echo mkdir ipa dir
mkdir -p "${IPA_DIR}"
fi

#删除老的archive,archive文件一般过大，因此删除，之保留最新的一个
if [ -d "${ARCHIVE_PATH}" ]
then
echo remove archive file
rm -rf ${ARCHIVE_PATH}
fi

#删除前一版本的cur ipa
if [ -f "${DEF_IPA_PATH}" ]
then
echo remove cur ipa file
rm -rf ${DEF_IPA_PATH}
fi

echo "start xcodebuild"


xcodebuild archive -workspace $WSNAME -scheme $SCHEME -configuration $CONFIGURATION -archivePath "${ARCHIVE_PATH}"

if [ -d "${ARCHIVE_PATH}" ]
then
xcodebuild -allowProvisioningUpdates  -exportArchive -archivePath "${ARCHIVE_PATH}" -exportPath "${IPA_DIR}" -exportOptionsPlist "${OPTIONS_PLIST_PATH}"
else
echo archive failed ${ARCHIVE_PATH}
exit
fi

IPA_PATH=$IPA_DIR/$SCHEME.ipa
if [ -f "${IPA_PATH}" ]
then
echo copy to def
cp $IPA_PATH $DEF_IPA_PATH
else
echo no ipa:${IPA_PATH}
fi



}




